package com.mcf.xiaolinju.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mcf.xiaolinju.vo.noticeReturn;
import com.mcf.xiaolinju.entity.Notices;
import com.mcf.xiaolinju.mapper.NoticesMapper;
import com.mcf.xiaolinju.service.INoticesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class NoticesServiceImpl extends ServiceImpl<NoticesMapper, Notices> implements INoticesService {
@Resource
    NoticesMapper noticesMapper;
//公告的添加
    public String add(int noticestype,String title,String notice,String starttime,String endtime,int villageid,int createid){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Notices notices=new Notices();
        Date date=new Date();
        LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDate startdate = LocalDate.parse(starttime, dateTimeFormatter);
        LocalDate enddate = LocalDate.parse(endtime, dateTimeFormatter);

        notices.setNoticestype(noticestype);
        notices.setTitle(title);
        notices.setNotice(notice);
        notices.setStarttime(startdate);
        notices.setEndtime(enddate);
        notices.setVillageid(villageid);
        notices.setCreateid(createid);
        notices.setCreatetime(ldt);
        if(noticesMapper.insert(notices)>0){
            return "添加成功";
        }
        return "添加失败";
    }
//修改公告
public String update(int id, String title, String content, String starttime, String endtime,  int userid){
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    LocalDate startdate = LocalDate.parse(starttime, dateTimeFormatter);
    LocalDate enddate = LocalDate.parse(endtime, dateTimeFormatter);
    Date date=new Date();
    LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    if(noticesMapper.update(id,title,content,startdate,enddate,userid,ldt)>0){
           return "修改成功";
    }
    return "修改失败";
}
//删除公告
public String del(int id,int userid){
    Date date=new Date();
    LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    if(noticesMapper.del(id,userid,ldt)>0){
        return "删除成功";
    }
    return "删除失败";
}
//撤回公告
public String undo(int id,int userid){
    Date date=new Date();
    LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    if(noticesMapper.undo(id,userid,ldt)>0){
        return "撤回成功";
    }
    return "撤回失败";
}
//批量撤回
@Transactional(rollbackFor =  Exception.class) //回滚
    public String manyundo(Integer[] id,int updateid){
        Date date=new Date();
        LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        if(noticesMapper.manyundo(id,updateid,ldt)!=id.length){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "撤回失败";
        }
        else{
            return "撤回成功";
        }
    }
    //批量删除
    @Transactional(rollbackFor =  Exception.class)
    public String manydel(Integer[] id, int updateid) {
        Date date=new Date();
        LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        if(noticesMapper.manydel(id,updateid,ldt)!=id.length){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "删除失败";
        }
        else{
            return "删除成功";
        }
    }
    //模糊查询公告
    public Page<noticeReturn> selectdim(IPage<noticeReturn> page, int id, int type, String title, String time) {

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if(time!=""){
            LocalDate date = LocalDate.parse(time, dateTimeFormatter);
            return noticesMapper.selectdim(page,id,type,title,date);
        }
        LocalDate date=null;
        return noticesMapper.selectdim(page,id,type,title,date);
    }
//分页查询公告
    public Page<noticeReturn>  selectpage(IPage<noticeReturn> page, int id,int type){
    return noticesMapper.selectpage(page,id,type);
    }
//定时修改状态
    @Scheduled(cron="0 1 0 * * ?")
    public void everyupdate(){
        noticesMapper.everyupdate();
    }

    //小程序查询当天的公告
    public List<Notices> selectnotice(int id) {
        return  noticesMapper.selectnotice(id);
    }
    //小程序模糊查询小区的公告
    public List<Notices> selectdimnotice(int id,String noticeid,String date,String title) {
        if(noticeid!="") {
            int noticetype = Integer.parseInt(noticeid);
            if(date !="") {
                DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDate datatime = LocalDate.parse(date, fmt);
                return noticesMapper.selectdimnotice(id,noticetype,datatime,title);
            }else{
                LocalDate datetime=null;
                return noticesMapper.selectdimnotice(id,noticetype,datetime,title);
            }

        }
        Integer noticenum= null;
        if(date !="") {
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate datatime = LocalDate.parse(date, fmt);
            return noticesMapper.selectdimnotice(id,noticenum,datatime,title);
        }

        LocalDate datetime=null;
        return noticesMapper.selectdimnotice(id,noticenum,datetime,title);
    }
}
